import fs from 'fs'
import { reflectType } from './mysqlreflect.js'
import { formatToUpHump, formatToHump } from './utils.js'
export const createComposable = (columns, tableName) => {
  fs.readFile('src/scripts/config.json', 'utf8', (err, data) => {
    if (err) throw err
    const config = JSON.parse(data)
    const folder = `${config.vendorFolder}/${tableName}`
    // verify file is exist or not
    const composable = `${folder}/${formatToHump(tableName)}.ts` // 添加组合函数
    if (fs.existsSync(composable)) {
      // file is exist
      console.log(`======file is existed and ignored to created composable:${composable}=======`)
    } else {
      // file is not exist
      if (!fs.existsSync(folder)) {
        fs.mkdirSync(folder, { recursive: true }, (err) => {
          if (err) throw err
        })
      }
      const model = []
      let arr = []
      for (const i in columns) {
        const type = reflectType(columns[i])
        switch (type) {
          case 'number':
            if (columns[i].columnDefault !== null) {
              model.push(`${formatToUpHump(columns[i].columnName)}:${Number(columns[i].columnDefault)}`)
            } else {
              model.push(`${formatToUpHump(columns[i].columnName)}:0`)
            }

            break
          case 'String':
            if (columns[i].columnDefault !== null) {
              model.push(`${formatToUpHump(columns[i].columnName)}:'${(columns[i].columnDefault)}'`)
            } else {
              model.push(`${formatToUpHump(columns[i].columnName)}:''`)
            }

            break
          case 'Array<String>':
            if (columns[i].columnDefault !== null) {
              arr = columns[i].columnDefault.split(',')
            }
            model.push(`${formatToUpHump(columns[i].columnName)}:${JSON.stringify(arr)}`)

            break
        }
      }
      fs.writeFileSync(composable, getContent(tableName, model), { flag: 'a' }, (err) => {
        if (err) {
          console.log('========error======')
          throw err
        } else {
          console.log(`======succeessfully created composable:${composable}=======`)
        }
      })
    }
  })
}
const getContent = (tableName, model) => {
  const obj = formatToHump(tableName)
  tableName = 'get' + formatToUpHump(tableName)
  return `
import { Form } from './form'
import { ref } from 'vue'
import { add, del, update, get } from './restful'
export const ${tableName} = () => {
  const initData:Form = {\n\t${model.join(',\n\t')}\n}
  const ${obj} = ref(initData)
  const get${tableName} = () => {
    get(${obj}.value.Id).then((res:any) => {}).catch(() => {})
  }
  const add${tableName} = () => {
    add(${obj}.value).then((res:any) => {}).catch(() => {})
  }
  const update${tableName} = () => {
    update(${obj}.value).then((res:any) => {}).catch(() => {})
  }
  const del${tableName} = () => {
    del(${obj}.value.Id).then((res:any) => {}).catch(() => {})
  }
  return {
    ${obj},
    get${tableName},
    add${tableName},
    update${tableName},
    del${tableName}
  }
}

`
}
